//
//  LongPickerView.m
//  CoachsBook
//
//  Created by huiyong li on 2022/8/19.
//

#import "LongPickerView.h"
#import "LongPickerViewModel.h"
@interface LongPickerView()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (strong, nonatomic) UIView *bjview;
@property (nonatomic,strong) UIButton *closeBut;
@property (nonatomic,strong) UIButton *determineBut;
@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) NSString *selstr;
@property (nonatomic,assign) NSInteger selrow;

@end

@implementation LongPickerView

- (instancetype)initWithFrame:(CGRect)frame dataArr:(NSArray *)dataArr
{
    self = [super initWithFrame:frame];
    if (self) {
        _dataArr = dataArr;
        LongPickerViewModel *model = self.dataArr.firstObject;
        self.selstr = model.title;
        self.selrow = 0;
        self.pickerViewType = LongSingleLine;
        [self setViews];
    }
    return self;
}

// 重写set属性
- (void)setDataArr:(NSArray *)dataArr{
    _dataArr = dataArr;
    [self updateData];
}

- (void)setPickerViewType:(LongPickerViewType)pickerViewType
{
    _pickerViewType = pickerViewType;
    [self updateData];
}


-(void)updateData{
    [self.mypickerView removeFromSuperview];
    [self MyPickerView];
    LongPickerViewModel *model = self.dataArr.firstObject;
    self.selstr = model.title;
    self.selrow = 0;
    [self.mypickerView selectRow:0 inComponent:0 animated:NO];
    [self.mypickerView reloadAllComponents];
}

-(void)setViews{
    
    self.backgroundColor = rgba(150, 150, 150, 0.6);
    self.bjview = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height-290, APPwidth, 290)];
    self.bjview.backgroundColor = [UIColor whiteColor];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bjview.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerTopLeft cornerRadii:CGSizeMake(12, 12)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bjview.bounds;
    maskLayer.path = maskPath.CGPath;
    self.bjview.layer.mask = maskLayer;
    [self addSubview:self.bjview];
    
    
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 10, APPwidth-120, 30)];
    self.titleLabel.text = @"请选择";
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.bjview addSubview:self.titleLabel];
    
    
    self.closeBut = [[UIButton alloc]initWithFrame:CGRectMake(15, 10, 40, 35)];
    [self.closeBut setTitle:@"取消" forState:UIControlStateNormal];
    [self.closeBut setTitleColor:rgba(153, 153, 153, 1) forState:UIControlStateNormal];
    self.closeBut.titleLabel.font = [UIFont systemFontOfSize:17];
    [self.closeBut addTarget:self action:@selector(closeButCheng:) forControlEvents:UIControlEventTouchUpInside];
    [self.bjview addSubview:self.closeBut];
    
    
    self.determineBut = [[UIButton alloc]initWithFrame:CGRectMake(APPwidth-55, 10, 40, 35)];
    [self.determineBut setTitle:@"确定" forState:UIControlStateNormal];
    [self.determineBut setTitleColor:rgba(22, 120, 255, 1) forState:UIControlStateNormal];
    self.determineBut.titleLabel.font = [UIFont systemFontOfSize:17];
    [self.determineBut addTarget:self action:@selector(determineButCheng:) forControlEvents:UIControlEventTouchUpInside];
    [self.bjview addSubview:self.determineBut];
    
    
    
}
#pragma mark - 设置选择器
-(void)MyPickerView
{
    UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 45, APPwidth, 245)];
    pickerView.delegate = self;
    pickerView.dataSource = self;
    [self.bjview addSubview:pickerView];
    self.mypickerView = pickerView;
    
}


#pragma mark - dataSouce
//有几行
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}
//行中有几列
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.dataArr.count;
}
//列显示的数据
//- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger) row forComponent:(NSInteger)component {
//    return self.dataArr[row];
//}

#pragma mark - delegate

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    if (self.pickerViewType == LongSingleLine) {
        return 40;
    }
    return 70;
}
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    LongPickerViewModel *model = self.dataArr[row];
    if (self.pickerViewType == LongSingleLine) {
        UILabel *titlelabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, APPwidth, 40)];
        titlelabel.text = model.title;
        titlelabel.textAlignment = NSTextAlignmentCenter;
        titlelabel.textColor = rgba(51, 51, 51, 1);
        titlelabel.font = [UIFont systemFontOfSize:16];
        return titlelabel;
    }
    UIView *cellview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, APPwidth, 70)];
    UILabel *titlelabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, APPwidth, 30)];
    titlelabel.text = model.title;
    titlelabel.textAlignment = NSTextAlignmentCenter;
    titlelabel.textColor = rgba(51, 51, 51, 1);
    titlelabel.font = [UIFont systemFontOfSize:16];
    
    UILabel *promptlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, APPwidth, 20)];
    promptlabel.text = model.prompt;
    promptlabel.textAlignment = NSTextAlignmentCenter;
    promptlabel.textColor = rgba(153, 153, 153, 1);
    promptlabel.font = [UIFont systemFontOfSize:12];
    [cellview addSubview:titlelabel];
    [cellview addSubview:promptlabel];
//    cellview.backgroundColor = rgba(229, 240, 255, 1);
    return cellview;

}

// 选中某一组的某一行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    LongPickerViewModel *model = self.dataArr[row];
    self.selstr = model.title;
    self.selrow = row;

}



-(void)determineButCheng:(UIButton *)but
{
    if ([self.delegate respondsToSelector:@selector(choosPickerView:WithRowStr:AndRowNum:)]) {
        [self.delegate choosPickerView:self WithRowStr:self.selstr AndRowNum:self.selrow];
    }
    self.hidden = YES;
}
-(void)closeButCheng:(UIButton *)but
{
    if ([self.delegate respondsToSelector:@selector(closePickerView:)]) {
        [self.delegate closePickerView:self];
    }
    self.hidden = YES;
}

@end
